package com.example.venadaoa.activity.setting;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.venadaoa.R;
import com.example.venadaoa.base.BaseActivity;
import com.example.venadaoa.datamanager.NoticeBean;
import com.example.venadaoa.datamanager.ServerResult;
import com.example.venadaoa.net.Api;
import com.example.venadaoa.net.NetUtils;
import com.example.venadaoa.tool.XListView;
import com.example.venadaoa.tool.XListView.IXListViewListener;
import com.mcxtzhang.commonadapter.lvgv.CommonAdapter;
import com.mcxtzhang.commonadapter.lvgv.ViewHolder;
import java.text.SimpleDateFormat;
import java.util.Date;

public class NoticeListActivity extends BaseActivity implements OnClickListener, OnItemClickListener, IXListViewListener {

    private Context context;
    private LayoutInflater inflater;
    private View main_view;
    //all my notRead
    private String type;
    private TextView title_content;
    private XListView list;
    private int currentPage = 1;
    public static Handler mHandler;
    private boolean arriveEnd = false;
    private boolean shouldRefresh = false;
    private CommonAdapter<NoticeBean.NotificationListBean> adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mPageName = "NoticeListActivity";
        // 初始化
        context = this;
        inflater = LayoutInflater.from(this);
        main_view = inflater.inflate(R.layout.my_task_list_view, null);
        setContentView(main_view);
        type = getIntent().getStringExtra("TYPE_NOTICE");
        title_content = (TextView) findViewById(R.id.title_content);
        list = (XListView) findViewById(R.id.lists);
        list.setPullLoadEnable(true);
        list.setXListViewListener(this);
        list.setOnItemClickListener(this);
        adapter = new CommonAdapter<NoticeBean.NotificationListBean>(context, null, R.layout.notice_list_item) {


            @Override
            public void convert(ViewHolder viewHolder, NoticeBean.NotificationListBean notificationListBean, int i) {
                TextView text;
                text = (TextView) viewHolder.getView(R.id.taskid_name);
                text.setText(notificationListBean.getCreate_by());

                text = (TextView) viewHolder.getView(R.id.title);
                text.setText(notificationListBean.getTitle());

                text = (TextView) viewHolder.getView(R.id.create_time);
                text.setText(notificationListBean.getCreate_time());

            }
        };
        list.setAdapter(adapter);
        //
        // 返回键
        ImageView backBtn = (ImageView) findViewById(R.id.title_bar_btn_left);
        // 刷新
        ImageView refreshBtn = (ImageView) findViewById(R.id.title_bar_btn_right);
        backBtn.setOnClickListener(this);
        refreshBtn.setOnClickListener(this);

        initList();

//		mHandler = new Handler(){
//	        public void handleMessage(Message msg) {
//	        	final int NOW_ID = msg.arg2;
//	        	//刷新公告
//	        	NoticeListActivity.this.runOnUiThread(new Runnable(){
//					@Override
//					public void run() {
//						DeleteTask dele = new DeleteTask(context,NOW_ID);
//						dele.execute();
//
//					}
//					});
//	        };
//	    };
    }

    private void initList() {
        if (type.equals("all")) {
            title_content.setText("公告管理");
        } else if (type.equals("my")) {
            title_content.setText("我的公告");

        }
        if (type.equals("notRead")) {
            title_content.setText("未读公告");
            shouldRefresh = true;
        }

        getNoticeList();
        if (type.equals("all")) {
//			RelativeLayout scroll = (RelativeLayout)main_view.findViewById(R.id.scorllView);
//			ImageBtnMove btn = new ImageBtnMove(this);
//			btn.setActivity(this);
//			btn.setListen(this);
//			btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.add));
//			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(100, 100);
//	        layoutParams.topMargin=200;
//	        layoutParams.leftMargin=8;
//	        layoutParams.rightMargin=8;
//	        layoutParams.bottomMargin=8;
//			scroll.addView(btn,layoutParams);
        }


    }

    private void getNoticeList() {
        Api.getNoticeList(context, type, currentPage + "", NoticeBean.class, new NetUtils.NetCallBack<ServerResult>() {
            @Override
            public void success(ServerResult rspData) {
                NoticeBean noticeBean = (NoticeBean) rspData;
                if (!noticeBean.getArriveEnd().equals("false")) {
                    list.removeFooterViews();
                    arriveEnd = true;
                }
                adapter.addDatas(noticeBean.getNotificationList());
                onLoad();
            }

            @Override
            public void failed(String msg) {

            }
        });
    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        int id = arg0.getId();
        switch (id) {
            case R.id.title_bar_btn_left:
                finish();
                break;

            case R.id.title_bar_btn_right:
                adapter.setDatas(null);
                currentPage = 1;
                getNoticeList();
                break;

            default:
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        System.out.println("详情" + arg2);
//		Intent intent = new Intent(context, NoticeDetailActivity.class);
//		intent.putExtra("NOTICE_NUM", ""+arg2);
//		intent.putExtra("should_refresh", shouldRefresh);
//		startActivityForResult(intent, 5);

    }

    @Override
    public void onRefresh() {
        list.addFooterViews();
        currentPage = 1;
        getNoticeList();

    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        if (!arriveEnd) {
            currentPage++;
            getNoticeList();
        } else {
            Toast.makeText(context, "已经到最后", Toast.LENGTH_SHORT).show();
            onLoad();
        }
    }

    private void onLoad() {
        SimpleDateFormat format = new SimpleDateFormat(
                "yyyy年MM月dd日  HH:mm");
        String date = format.format(new Date());
        list.stopRefresh();
        list.stopLoadMore();
        list.setRefreshTime(date);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {

            if (requestCode == 5) {
                onRefresh();
            }

        }

    }


}
